package online.indigo6a.gohttp;


import online.indigo6a.gohttp.core.Header;
import online.indigo6a.gohttp.core.Http;
import online.indigo6a.gohttp.parser.GoHttpBaseListener;
import online.indigo6a.gohttp.parser.GoHttpParser;

import java.util.ArrayList;
import java.util.List;

public class MyHttpListener  extends GoHttpBaseListener {

    List<Http> https;
    Http curHttp;
    List<Header> headers;
    Header curHeader;

    @Override
    public void enterComment(GoHttpParser.CommentContext ctx) {
        System.out.println("comment:"+"s"+ctx.getText()+"e");
    }

    @Override
    public void enterHttps(GoHttpParser.HttpsContext ctx) {
        super.enterHttps(ctx);
        https = new ArrayList<>();
    }

    @Override
    public void enterHttp(GoHttpParser.HttpContext ctx) {
        super.enterHttp(ctx);
        System.out.println("enter http");
        curHttp = new Http();
        headers = new ArrayList<>();
    }

    @Override
    public void exitHttp(GoHttpParser.HttpContext ctx) {
        super.exitHttp(ctx);
        System.out.println("exit http");
        curHttp.setHeaders(headers);
        System.out.println("cur:"+curHttp.getUrl().trim());
        https.add(curHttp);
    }

    @Override
    public void enterMethod(GoHttpParser.MethodContext ctx) {
        curHttp.setMethod(ctx.getText());
    }

    @Override
    public void enterUrl(GoHttpParser.UrlContext ctx) {
        curHttp.setUrl(ctx.getText());
    }


    @Override
    public void enterField_name(GoHttpParser.Field_nameContext ctx) {
        curHeader = new Header();
        String name = ctx.getText();
        curHeader.setName(name);
        System.out.println("field name="+name);
    }

    @Override
    public void enterField_value(GoHttpParser.Field_valueContext ctx) {
        System.out.println("ente value:"+ctx.getText());
        String value = ctx.getText();
        curHeader.setValue(value);
        headers.add(curHeader);
    }

    @Override
    public void enterHeader_field(GoHttpParser.Header_fieldContext ctx) {
        String name = ctx.getChild(0).getText();
        String value = ctx.getChild(1).getText();
        System.out.println(name+"="+value);
    }

    @Override
    public void enterBody(GoHttpParser.BodyContext ctx) {
        curHttp.setBody(ctx.getText());
    }

    @Override
    public void enterRequest(GoHttpParser.RequestContext ctx) {
        super.enterRequest(ctx);
    }

    public List<Http> getHttps() {
        return  https;
    }
}
